package com.itheima.controller;


import com.itheima.anno.Log;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {

    @Autowired
    private ClazzService clazzService;
    /**
     * 根据id查询班级
     */
    @GetMapping("/{id}")
    public Result getByIdClazz(@PathVariable Integer id) {
        log.info("id:{}", id);
        Clazz clazz=clazzService.getByIdClazz(id);

        return Result.success(clazz);
    }

    /**
     * 修改班级
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        clazzService.updateClazz(clazz);
        return Result.success();
    }
    /**
     * 查询所有班级
     */
    @GetMapping
    public Result clazzList(ClazzQueryParam clazzQueryParam){
        //调用service方法
        PageBean clazzes = clazzService.selectAllClazz(clazzQueryParam);
        return Result.success(clazzes);
    }

    /**
     * 查询学员管理中条件查询中的所有班级
     * @return
     */
    @GetMapping("/list")
    public Result clazzlist(){
       List<Clazz> clazzes= clazzService.findAll();
        return Result.success(clazzes);
    }

    /**
     * 添加班级
     */
    @Log
    @PostMapping()
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("add clazz:{}",clazz);
        clazzService.addClazz(clazz);

        return Result.success();
    }

    /**
     * 删除班级
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id){
         clazzService.deleteById(id);

            return Result.success();


    }
}
